<?php
/*
@auther: bigeyex@gmail.com
This class is designed for rendering a page.
argument $header, $footer, $columns is passed in the form of modules
eg.$page->columns=array(
                    //each array stands for a column
                    array(
                        //each array here stands for a module
                        array('name'=>'Testmodule','arg'=>''),
                        array('name'=>'Testmodule','arg'=>'')
                    ),
                    array(
                        array('name'=>'Testmodule','arg'=>''),
                        array('name'=>'Testmodule','arg'=>''),
                        array('name'=>'Testmodule','arg'=>'')
                    ),
                    array()
                );
after all of this, call ->render() to get the full string
    or call show() to directly show it on the page.
 */
require_once(PLF::$class_path . "PLFTemplate.php");


class PLFPage{

    //this page_name is the key of page layout stored in the database
    public $page_name="DefaultPage";

    //(PLFModule)header part of the page
    //it will be shown in the beginning of <body> tag.
    public $header;

    //(PLFModule)footer part of the page
    //shown in the end of <body> tag
    public $footer;

    //(String)title of the page.
    //shown in the <title> tag of <head>
    public $title;

    //(String)the filename (no extension) of the template file.
    //file PLF::$theme_path . $this->theme / $this->template .tpl.php
    //will be loaded.
    public $template ='doc';

    //(PLFModule)columns of the page.
    public $columns;

    //a list of css loaded on the top of the page.
    public $css_list=array();

    //theme name, file in PLF::$theme_path . $this->theme / will be loaded
    //as html files.
    public $theme="Default";

    public function render(){
        //this is the page template
        $tpl = new PLFTemplate(PLF::$theme_path . $this->theme . "/" .
        	 $this->template . ".tpl.php");
        
        $tpl->set('theme_path',PLF::$theme_path . $this->theme . "/");
        $tpl->set('page_title',$this->title);
        $tpl->set('page_name',$this->page_name);
        $tpl->set('css_list',$this->css_list);

        //html of page content(including modules)
        $innerhtml="";

        //render the header part of the page
  		foreach($this->header as $widget){
            require_once(PLF::$widget_path . $widget . "/" . $widget . ".php");
            $obj = new $widget;
            $innerhtml .= $obj->render();
        }

        
        $tpl->set('page_head',$innerhtml);
        $innerhtml="";
        //this variable is used to generate column id (eg:id="column1" ...)
        $num_columns=1;

        foreach($this->columns as $column){
            //each column is a ul element while each module is a li.
            $innerhtml .= '<ul class="column" id="column'.$num_columns.'">
                ';
            foreach($column as $widget){
                require_once(PLF::$widget_path . $widget['name'] . "/" . $widget['name'] . ".php");
                $obj = new $widget['name'];
                $obj->arg=$widget['arg'];
                $innerhtml .= $obj->render();
            }
            $innerhtml .= '</ul>';
            $num_columns++;
        }
        $tpl->set('page_columns_content',$innerhtml);
            
        return $tpl->fetch();

    }
    
    public function show(){
    	echo $this->render();
    }

}

?>
